package cs.upt.awp.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cs.upt.awp.data.Status;
import cs.upt.awp.data.TravelNote;
import cs.upt.awp.data.TravelRecord;
import cs.upt.awp.model.TravelRecordViewModel;
import cs.upt.awp.services.TravelNotesServices;
import cs.upt.awp.services.TravelRecordsServices;

@Controller
@RequestMapping("record/{id}/notes")
public class TravelNotesController {

	@Autowired
	TravelNotesServices notesServices;
	
	@Autowired
	TravelRecordsServices recordsServices;
	
	@RequestMapping(value = "/addnote", method = RequestMethod.GET)
	public ModelAndView addNoteGet(@PathVariable("id") long id){
		ModelAndView model = new ModelAndView();
		TravelRecord tr = null;
		try {
			 tr = recordsServices.getEntityById(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TravelNote tn = new TravelNote();
		tn.setRecord(tr);
		model.addObject("travelNote", tn);
		model.setViewName("addNote");
		return model;
	}
	
	@RequestMapping(value = "/addnote", method = RequestMethod.POST)
	public String addNote(@PathVariable("id") long id, @ModelAttribute("travelNote") TravelNote travelNote){		
		System.out.println("note:" + travelNote.toString());
		try {
			TravelNote tn = new TravelNote();
			tn.setCreationDate(new Date());
			tn.setTitle(travelNote.getTitle());
			tn.setNote(travelNote.getNote());
			tn.setRecord(recordsServices.getEntityById(id));	
			notesServices.addEntity(tn);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:/records/updaterecord/"+id;
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public @ResponseBody TravelNote getTravelNote(@PathVariable("id") long id){
		TravelNote note = null;
		try {
			note = notesServices.getEntityById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return note;
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public @ResponseBody List<TravelNote> getAllTravelNotes(){
		List<TravelNote> notes = null;
		try {
			notes = notesServices.getEntityList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return notes;
	}
	
	@RequestMapping(value="/deletenote/{noteId}", method=RequestMethod.GET)
	public String deleteTravelNote(@PathVariable("id") long id, @PathVariable("noteId") long noteId){
		try {
			notesServices.deleteEntity(noteId);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/records/updaterecord/"+id;
	}
	
	@RequestMapping(value="/editnote/{noteId}", method=RequestMethod.GET)
	public ModelAndView editTravelNoteGet(@PathVariable("id") long id, @PathVariable("noteId") long noteId){
		ModelAndView model = new ModelAndView();
		try {
			model.addObject("note",notesServices.getEntityById(noteId));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("editNote");
		return model;
	}
	
	@RequestMapping(value="/editnote/{noteId}", method=RequestMethod.POST)
	public String editTravelNote(@PathVariable("id") long id, @PathVariable("noteId") long noteId, @ModelAttribute("note") TravelNote note){
		ModelAndView model = new ModelAndView();
		try {
			TravelNote tn = notesServices.getEntityById(noteId);
			tn.setTitle(note.getTitle());
			tn.setNote(note.getNote());
			notesServices.updateNote(tn);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("editNote");
		return "redirect:/records/updaterecord/"+id;
	}
}
